package cn.ilikes.tools.hbase.jpa.configs;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.slf4j.Logger;

import cn.ilikes.tools.hbase.jpa.JPAHbasePersistenUnitEnv;
import cn.ilikes.tools.hbase.jpa.SysConfigs;
import cn.ilikes.tools.hbase.jpa.configs.tools.JPAHBaseConfigurationPaserException;
import cn.ilikes.tools.hbase.jpa.configs.tools.PersistenceXMLLoader;
import cn.ilikes.tools.hbase.jpa.metadatas.IORMMetaDataFactory;
import cn.ilikes.tools.hbase.jpa.metadatas.JPAHBasePersistenceUnitMetadata;
import cn.ilikes.tools.hbase.jpa.metadatas.orms.IORMPaser;
import cn.ilikes.tools.hbase.jpa.metadatas.orms.ORMMetaDataFactory;
import cn.ilikes.tools.hbase.jpa.metadatas.orms.ORMPaser;
import cn.ilikes.tools.hbase.jpa.metadatas.orms.elements.EntityMappings;
import cn.ilikes.tools.hbase.jpa.metadatas.orms.pasers.DOM4JPaserException;
import cn.ilikes.tools.logs.Loggerfactory;

/**
 * 主要解析 persistence file,解析成功后加载装入PersistenceUnitMetadataFactory。
 * 
 * @author fack
 */
public class PersistenceUnitConfigure implements IConfigure {

	private static Logger logger = Loggerfactory.instance(Configure.class);

	public PersistenceUnitConfigure(Properties configs) {
		Loggerfactory.info(logger, "load Persistence Unit Configure ......");
	}

	@Override
	public void config(String persistenceUnitsName, JPAHbasePersistenUnitEnv hbasePersistenUnitEnv) throws JPAHBaseConfigurationException {
		{
			Loggerfactory.info(logger, "Persistence Unit Configure Start Paser PersistenceUnitsName  : " + persistenceUnitsName);
			if (persistenceUnitsName == null)
				throw new JPAHBaseConfigurationPaserException("persistence units name not find ; please set name in persistence.xml ");
			try {
				String persistencefile = SysConfigs.persistencefile;
				PersistenceXMLLoader persistenceXMLLoader = new PersistenceXMLLoader(persistenceUnitsName);
				Map<String, JPAHBasePersistenceUnitMetadata> metadatas = persistenceXMLLoader.paserPersistenceMetadatas(persistencefile);
				JPAHBasePersistenceUnitMetadata hBasePersistenceUnitMetadata = metadatas.get(persistenceUnitsName);
				if (hBasePersistenceUnitMetadata != null) {
					hbasePersistenUnitEnv.sethBasePersistenceUnitMetadata(hBasePersistenceUnitMetadata);
					paserMappingfile(hBasePersistenceUnitMetadata.getMapfile(), hbasePersistenUnitEnv);

				} else {
					throw new JPAHBaseConfigurationPaserException("not find  persistenceUnitsName : " + persistenceUnitsName + "  Persistence Unit Configure ");
				}

				Loggerfactory.info(logger, "finishing persistence file : " + persistencefile);

			} catch (JPAHBaseConfigurationPaserException icex) {
				Loggerfactory.error(logger, "Error occurred during persistence unit configuration, Caused by:" + icex.getMessage());
				throw new JPAHBaseConfigurationException(icex);
			}
			Loggerfactory.info(logger, "end   paser  persistence unit    persistenceUnitsName  : " + persistenceUnitsName);
		}

	}

	private void paserMappingfile(List<String> mapfiles, JPAHbasePersistenUnitEnv hbasePersistenUnitEnv) throws JPAHBaseConfigurationException {
		ORMMetaDataFactory ormetaDataFactory = new ORMMetaDataFactory();
		IORMPaser ormpaser = new ORMPaser(ormetaDataFactory);
		for (String mapfile : mapfiles) {
			try {
				ormpaser.paser(mapfile);
			} catch (DOM4JPaserException e) {
				throw new JPAHBaseConfigurationException(e);
			}
		}
		hbasePersistenUnitEnv.setOrmetaDataFactory(ormetaDataFactory);
	}

}
